import java.util.stream.Stream;

/**
 * Created with IntelliJ IDEA.
 * Description:异常
 * User: XMQ
 * Date: 2022-05-31
 * Time: 8:36
 */
class Person implements Cloneable{
    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class Test {

    public static void main10(String[] args) {
        try{

        }catch (NullPointerException e){
            System.out.println("空指针异常");
        }catch (ArithmeticException e){
            System.out.println("算术异常");
        }
    }
    //throw用来抛出异常，一般用作程序员自定义的异常
    public static void func2(int a){
        if(a == 0){
            throw new RuntimeException("自定义异常");
        }
    }
    public static void main9(String[] args) {
        func2(0);
    }
    public static void main8(String[] args) {
        //一旦抛出异常，其后的代码逻辑就不会执行了
        int[] arr = null;
        System.out.println(arr.length);
        System.out.println("其后的代码逻辑");
    }
    //受查异常  必须要处理受查异常，声明或者捕获
    public static void main7(String[] args) throws CloneNotSupportedException {
        Person person1 = new Person();
        Person person2 = (Person) person1.clone();
    }
    public static void func(){
        func();
    }
    //错误error
    public static void main6(String[] args) {
        func();
    }
    //运行时异常，未受查异常
    public static void main5(String[] args) {
        int[] arr = null;
        System.out.println(arr.length);
        int[] array = new int[3];
        System.out.println(array[10]);
    }
    public static void main4(String[] args) {

        System.out.println(10/0);
    }

    public static String addStrings(String num1, String num2) {
        //模拟大数计算
        int i = num1.length() - 1, j = num2.length() - 1, add = 0;
        StringBuffer ans = new StringBuffer();
        while (i >= 0 || j >= 0 || add != 0) {
            int x = i >= 0 ? num1.charAt(i) - '0' : 0;
            int y = j >= 0 ? num2.charAt(j) - '0' : 0;
            int result = x + y + add;
            ans.append(result % 10);
            add = result / 10;
            i--;
            j--;
        }
        // 计算完以后的答案需要翻转过来
        ans.reverse();
        return ans.toString();
    }
    public static void main3(String[] args) {
        String s1 = "123";
        String s2 = "11";
        System.out.println(addStrings(s1, s2));
    }
    public static int StrToInt(String str) {
        int len = str.length();
        int flag = 1;//符号位
        int nums = 0;//返回值
        int i = 0; //遍历字符串
        int radix = 10;//要返回的进制 我要调用digit方法
        if(len == 0) return 0;
        char firstchar = str.charAt(0);//字符串第一位

        //确定第一位是否合法
        if((firstchar != '-' && firstchar != '+')
                && (str.charAt(i) < '0' || str.charAt(i) > '9' )){
            return 0;
        }
        //确定符号位
        if(firstchar == '-' || firstchar == '+'){
            if(firstchar == '-'){
                flag = -1;
            }
            i++;
        }

        while( i < len){
            //判断是否有非法字符
            if(str.charAt(i) < '0' || str.charAt(i) > '9'){
                return 0;
            }
            //转化为整数
            nums *= radix;
            nums += Character.digit(str.charAt(i),radix);
            i++;
        }
        return flag*nums;
    }
    public static void main2(String[] args) {
        String s1 = "+12313";
        System.out.println(StrToInt(s1));
    }
    public static void main1(String[] args){
        String s1 = "+23";
        String s2 = String.valueOf(123);
        int num = Integer.parseInt(s2);
        int nums = 0;
        int radix = 10;
        if(s1.length() == 0) {
            System.out.println("0");
        }
        for (int i = 0; i < s1.length(); i++) {
            nums *= radix;
            System.out.println(nums);
            nums += Character.digit(s1.charAt(i), radix);
            System.out.println(nums);
            System.out.println("==========");;
        }
        System.out.println(nums);
//        int j = Character.digit(s1.charAt(1), 10);
//        System.out.println(j);
//        System.out.println(num);
    }
}
